package com.fly.lambda.dao;

import com.fly.lambda.domain.Message;
import com.fly.lambda.framework.dao.BaseDAO;
import com.fly.lambda.framework.dao.Pagination;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fly on 2016/7/29.
 */
@Repository
public class MessageDAO extends BaseDAO<Message> {

    public int queryMessageCount(String status){
        StringBuilder hql = new StringBuilder("from Message where 1 = 1");
        List<Object> params = new ArrayList<Object>();
        if (StringUtils.isNotBlank(status)){
            hql.append(" and status = ?");
            params.add(status);
        }
        hql.append(" order by create_time desc");
        return count(hql.toString(), true, params.toArray());
    }

    public Pagination<Message> queryMessagePage(String read, int curPage, int pageSize){
        StringBuilder hql = new StringBuilder("from Message where 1 = 1");
        List<Object> params = new ArrayList<Object>();
        if (StringUtils.isNotBlank(read)){
            hql.append(" and is_read = ?");
            params.add(read);
        }
        hql.append(" order by create_time desc");
        return doQueryPage(hql.toString(), true, curPage, pageSize, params.toArray());
    }

    public int unreadCount(){
        String hql = "from Message where is_read = ?";
        return count(hql, true, Message.READ_NO);
    }

}
